import { config } from '../config.js';
import { MyModel } from '../../../modules/my.js';
import { config as api } from '../../../config.js';

const app = getApp();
const my = new MyModel();

Page({
  data: {
    imageUrl: '/images/my/add_iimage.png',
    headImage: null,
    sexArray: config.data.sexArray, // 年龄模板
    experience: config.data.experience,  // 工作经验模板
    joinstauts: config.data.joinstauts, // 求职状态模板
    edu: config.data.edu, // 最高学历模板
    politics: config.data.politics, // 政治面貌模板
    sex: null,  // 性别
    birthday: null, // 出生日期
    worddate: null, // 工作经验
    jobStatus: null, // 求职状态
    maxedu: null, // 最高学历
    politic: null // 政治面貌
  },
  addImage: function(e) {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        wx.uploadFile({
          url: api.IMAGE_URL + 'app/aliossUpload',
          header: {
            "Content-Type": "multipart/form-data"
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {},
          success: result => {
            res = JSON.parse(result.data);
            if(res) {
              _this.setData({
                headImage: res.url
              })
            }else {
              wx.showToast({
                title: '上传失败,请稍后重试！',
                icon: 'none'
              })
            }
          },
          fail: err => {
            console.log('上传头像失败：', err)
          },
        });
      },
      fail: err => {
        console.log(err);
      }
    });
  },
  bindChange: function(e) {
    let _this = this;
    switch (e.target.id) {
      // 性别
      case 'sex':
        _this.setData({
          sex: e.detail.value
        });
      break;
      // 出生日期
      case 'birthday':
        _this.setData({
          birthday: e.detail.value
        });
      break;
      // 工作经验
      case 'worddate':
        _this.setData({
          worddate: e.detail.value
        });
      break;
      // 求职状态
      case 'joinstauts':
        _this.setData({
          jobStatus: e.detail.value
        });
      break;
      // 最高学历
      case 'maxedu':
        _this.setData({
          maxedu: e.detail.value
        });
      break;
      // 政治面貌
      case 'politics':
        _this.setData({
          politic: e.detail.value
        });
      break;
      default :
      break;
    }
  },
  // 保存简历
  saveResume(e) {
    let value = e.detail.value;
    let data = {
      openid: app.getOpenId(),
      heardImage: this.data.headImage,
      name: value.name || null,
      sex: this.data.sex,
      telphone: value.phone || null,
      birthday: this.data.birthday,
      workAge: this.data.worddate,
      jobStatus: this.data.jobStatus,
      education: this.data.maxedu,
      specialty: value.specialty || null,
      nativePlace: value.nativeplace || null,
      hopeWorkAddress: value.address || null,
      politicsStatus: this.data.politic,
      email: value.email || null,
      selfDesc: value.evaluation || null
    }
    // 验证是否填写
    if(data['name'] == null || data['name'].trim() == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      });
      return false
    }else if(data['telphone'] == null || data['telphone'].trim() == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      });
      return false
    }else if(data['sex'] == null) {
      wx.showToast({
        title: '性别不能为空',
        icon: 'none'
      });
      return false
    }else if(data['birthday'] == null) {
      wx.showToast({
        title: '出生日期不能为空',
        icon: 'none'
      });
      return false
    }
    if(data['email']) {
      if(!new RegExp(/^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g).test(data['email'])) {
        wx.showToast({
          title: '请输入正确的邮箱',
          icon: 'none'
        });
        return false
      }
    }
    console.log(data);
    app.isAuthize()
    .then(res => {
      if(res) {
        my.addResumne(data)
        .then(res => {
          console.log(res)
          if(res.code == 2) {
            wx.showToast({
              title: '保存成功!'
            })
            setTimeout(() => {
              my.setResume();
              wx.switchTab({
                url: '../index/index'
              })
            }, 2000)
          }else {
            wx.showToast({
              title: '保存失败!'
            })
          }
        })
      }
    })
  }
})